package com.wang.myapplication.network;

import android.util.Log;

import com.wang.myapplication.activity.MyHttpback;
import com.wang.myapplication.network.bean.BBSListsResponse;
import com.wang.myapplication.network.bean.ClassInfoListResponse;
import com.wang.myapplication.network.bean.GlobalConfigResponse;
import com.wang.myapplication.network.bean.KMInfoListResponse;
import com.wang.myapplication.network.bean.LoginResponse;
import com.wang.myapplication.network.bean.WildConfigResponse;
import com.wang.myapplication.tools.RetrofitUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;
import retrofit2.http.Url;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by xhwang on 2018/3/20.
 */

public class HttpRequest {
    private static Map<String, Retrofit> mRetrofitMap = new HashMap();

    public static Retrofit getRetrofitInstance(String baseUrl) {
        Retrofit result = null;
        synchronized (RetrofitUtils.class) {
            Retrofit retrofit = (Retrofit) mRetrofitMap.get(baseUrl);
            if (retrofit == null) {
                //声明日志类
                HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
                //设定日志级别
                httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                //自定义OkHttpClient
                OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
                //添加拦截器
                okHttpClient.addInterceptor(httpLoggingInterceptor);


                //创建并指定自定义的OkHttpClient
                retrofit = new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).client(okHttpClient.build())//此行为了log
                        .build();

                mRetrofitMap.put(baseUrl, retrofit);
            }
            result = retrofit;
        }
        return result;
    }


    public interface WSuserService {
        //http://112.74.195.215:100/VHKM/wsuser.asmx/Login?uid=wxq&pwd=wxq
        @GET("Login")
        Observable<LoginResponse> loginRequest(@Query("uid") String uid, @Query("pwd") String pwd);

    }

    public static void reqLoginRequest(final MyHttpback<LoginResponse> pMyback, String uid, String pwd) {
        WSuserService wSuserService = getRetrofitInstance("http://112.74.195.215:100/VHKM/wsuser.asmx/").create(WSuserService.class);
        wSuserService.loginRequest(uid, pwd).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new MySubscriber<LoginResponse>(pMyback));
    }

    public interface WSbbsService {
        //http://112.74.195.215:100/VHKM/wsbbs.asmx/getAllDatas?userid=23&tokenid=1&page=1&classid=0&typeid=1&key=&classtype=1
        @GET("getAllDatas")
        Observable<BBSListsResponse> getAllDatas(@Query("tokenid") String tokenid, @Query("userid") String userid, @Query("page") int page, @Query("classid") int classid, @Query("typeid") int typeid, @Query("key") String key, @Query("classtype") int classtype);

        //http://112.74.195.215:100/VHKM/wsbbs.asmx/getDetails?tokenid=1&con_id=6653 ---帖子发布内容详情
        @GET("getDetails")
        Observable<LoginResponse> getDetails(@Query("tokenid") String tokenid, @Query("con_id") int con_id);

        //http://112.74.195.215:100/VHKM/wsbbs.asmx/getCommentList?tokenid=1&con_id=6653&page=1 ---帖子评论详情
        @GET("getCommentList")
        Observable<LoginResponse> getCommentList(@Query("tokenid") String tokenid, @Query("con_id") int con_id, @Query("page") int page);

    }

    public static void reqGetbbsAllDatas(final MyHttpback<BBSListsResponse> pMyback, int page, int classid, int typeid, String key, int classtype) {
        WSbbsService wSbbsService = getRetrofitInstance("http://112.74.195.215:100/VHKM/wsbbs.asmx/").create(WSbbsService.class);
        String userid = "23";
        String tokenid = "1";
        wSbbsService.getAllDatas(userid, tokenid, page, classid, typeid, key, classtype).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new MySubscriber<BBSListsResponse>(pMyback));
    }


    public interface WSArchiveService {
        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetTradeList?token=1---行业动态
        @GET("GetTradeList")
        Observable<LoginResponse> GetTradeList(@Query("token") String token);

        // http://112.74.195.215:100/VHKM/WSArchive.asmx/GetZYZYList?token=1---作业指导
        @GET("GetZYZYList")
        Observable<LoginResponse> GetZYZYList(@Query("token") String token);

        // http://112.74.195.215:100/VHKM/WSArchive.asmx/GetNewProjectList?token=1---最新归档
        @GET("GetNewProjectList")
        Observable<LoginResponse> GetNewProjectList(@Query("token") String token);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetTopArchiveRank?Token=1&token=1&topCount=20---知识排行
        @GET("GetTopArchiveRank")
        Observable<LoginResponse> GetTopArchiveRank(@Query("token") String token, @Query("Token") String Token, @Query("topCount") int topCount);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetMessageCount?token=1&iEventID=-1&iUserId=23&iRead=0--未读消息
        @GET("GetMessageCount")
        Observable<LoginResponse> GetMessageCount(@Query("token") String token);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetMessage?token=1&iUserId=23&iEventID=-1&iStatus=-1&strKey=&iPage=1&startDT=&endDT=--消息列表
        @GET("GetMessage")
        Observable<LoginResponse> GetMessage(@Query("token") String token, @Query("strKey") String key, @Query("iUserId") int iUserId, @Query("iEventID") int iEventID, @Query("iStatus") int iStatus, @Query("iPage") int iPage, @Query("startDT") String startDT, @Query("endDT") String endDT);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetArchiveInfoListSort?token=1&iClassId=1&iArchiveId=1&iArchiveTypeId=0&key=&page=1&oType=3&iAsc=1--产品库中间内容
        @GET("GetArchiveInfoListSort")
        Observable<LoginResponse> GetArchiveInfoListSort(@Query("token") String token, @Query("key") String key, @Query("iClassId") int iClassId, @Query("iArchiveId") int iArchiveId, @Query("iArchiveTypeId") int iArchiveTypeId, @Query("page") int page, @Query("oType") int oType, @Query("iAsc") int iAsc);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetArchiveInfoListWhere?token=1&iClassId=15&iArchiveId=1&iArchiveTypeId=19&strWhere=&page=1&oType=1&iAsc=1---项目地图 所有项目
        @GET("GetArchiveInfoListWhere")
        Observable<LoginResponse> GetArchiveInfoListWhere(@Query("token") String token, @Query("strWhere") String strWhere, @Query("iClassId") int iClassId, @Query("iArchiveId") int iArchiveId, @Query("iArchiveTypeId") int iArchiveTypeId, @Query("page") int page, @Query("oType") int oType, @Query("iAsc") int iAsc);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetCondition?token=1&strWhereID=52---项目地图 搜索某个
        @GET("GetCondition")
        Observable<LoginResponse> GetCondition(@Query("token") String token, @Query("strWhereID") int strWhereID);



        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetArchiveDetail?token=1&iClassId=1&iArchiveId=9332&userId=23--文件内容详情
        @GET("GetArchiveDetail")
        Observable<LoginResponse> GetArchiveDetail(@Query("token") String token, @Query("iClassId") int iClassId, @Query("iArchiveId") int iArchiveId, @Query("userId") int userId);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetEMInfo?token=1&iClassId=15&iArchiveId=20--项目内容详情
        @GET("GetEMInfo")
        Observable<LoginResponse> GetEMInfo(@Query("token") String token, @Query("iClassId") int iClassId, @Query("iArchiveId") int iArchiveId);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetArchivePerm?token=1&iClassId=1&iArchiveId=9332&oprId=118&iUserId=23--文件的权限
        @GET("GetArchivePerm")
        Observable<LoginResponse> GetArchivePerm(@Query("token") String token, @Query("iClassId") int iClassId, @Query("iArchiveId") int iArchiveId, @Query("oprId") int oprId, @Query("iUserId") int iUserId);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetArchiveTypeDics?token=1&archiveTypeId=20---编号专业职称条目
        @GET("GetArchiveTypeDics")
        Observable<LoginResponse> GetArchiveTypeDics(@Query("token") String token, @Query("archiveTypeId") int archiveTypeId);

        //http://112.74.195.215:100/VHKM/WSArchive.asmx/GetMBuildList?token=1&filter=---专家达人列表
        @GET("GetMBuildList")
        Observable<LoginResponse> GetMBuildList(@Query("token") String token, @Query("filter") String filter);

    }

    public static void reqLoginRequest(final MyHttpback<LoginResponse> pMyback) {
        WSArchiveService wSArchiveService = getRetrofitInstance("http://112.74.195.215:100/VHKM/WSArchive.asmx/").create(WSArchiveService.class);
        String token = "1";
        wSArchiveService.GetTradeList(token).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new MySubscriber<LoginResponse>(pMyback));
    }


    public interface WSCommonService {
        //http://112.74.195.215:100/VHDWS/datapath/Common/globalConfig.json
        @GET("globalConfig.json")
        Observable<GlobalConfigResponse> globalConfig();

        //http://112.74.195.215:100/VHDWS/datapath/Common/wildConfig005.json//这种类型的直接写url
        @GET
        Observable<WildConfigResponse> wildConfig(@Url String url);

        //http://112.74.195.215:100/VHKM/WSCommon.asmx/GetClassInfoList?token=1---库类型【产品库、销售库、市场库】
        @GET("GetClassInfoList")
        Observable<ClassInfoListResponse> GetClassInfoList(@Query("token") String token);

        //http://112.74.195.215:100/VHKM/WSCommon.asmx/GetArchiveTypeList?token=1&iClassId=1&iArchiveId=1---文件类型 【文件夹、文件、产品文件】
        @GET("GetArchiveTypeList")
        Observable<LoginResponse> GetArchiveTypeList(@Query("token") String token, @Query("iClassId") int iClassId, @Query("iArchiveId") int iArchiveId);


        ////http://112.74.195.215:100/VHKM/WSCommon.asmx/GetKMInfoList?token=1---知识库\grid界面的接口、
        @GET("GetKMInfoList")
        Observable<KMInfoListResponse> GetKMInfoList(@Query("token") String token);

        //http://112.74.195.215:100/VHKM/WSCommon.asmx/GetAppInfo?token=1---项目地图 右侧栏条件
        @GET("GetAppInfo")
        Observable<ClassInfoListResponse> GetAppInfo(@Query("token") String token);

        ////http://112.74.195.215:100/VHDWS/WSCommon.asmx/GetArchiveMapDicList?token=1&iArchiveTypeId=31---收索右侧栏内容
        @GET("GetArchiveMapDicList")
        Observable<ClassInfoListResponse> GetArchiveMapDicList(@Query("token") String token, @Query("iArchiveTypeId") int iArchiveTypeId);

        //http://112.74.195.215:100/VHKM/WSCommon.asmx/GetArchiveCount?token=1---档案管理数目
        @GET("GetArchiveCount")
        Observable<LoginResponse> GetArchiveCount(@Query("token") String token);

        //http://112.74.195.215:100/VHKM/WSCommon.asmx/GetArchiveTypeStatisticList?token=1&year=2017&classId=undefined--年度统计
        @GET("GetArchiveTypeStatisticList")
        Observable<LoginResponse> GetArchiveTypeStatisticList(@Query("token") String token, @Query("year") String year, @Query("classId") int classId);

        //http://112.74.195.215:100/VHKM/WSCommon.asmx/GetAppClassInfoList?token=1&year=2017--种类统计
        @GET("GetAppClassInfoList")
        Observable<LoginResponse> GetAppClassInfoList(@Query("token") String token, @Query("year") String year);

        //http://112.74.195.215:100/VHKM/WSCommon.asmx/GetArchiveDetail?token=1&iArchiveTypeId=3---某个文件的类型 得到typeclass
        @GET("GetArchiveDetail")
        Observable<LoginResponse> GetArchiveDetail(@Query("token") String token, @Query("iArchiveTypeId") String iArchiveTypeId);


    }


    public static void reqGlobalConfig(final MyHttpback<GlobalConfigResponse> pMyback) {
        WSCommonService wsCommonService = getRetrofitInstance("http://112.74.195.215:100/VHDWS/datapath/Common/").create(WSCommonService.class);
        wsCommonService.globalConfig().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new MySubscriber<GlobalConfigResponse>(pMyback));
    }

    public static void reqWildConfig(final MyHttpback<WildConfigResponse> pMyback, String url) {
        WSCommonService wsCommonService = getRetrofitInstance("http://112.74.195.215:100/VHDWS/datapath/Common/").create(WSCommonService.class);
        wsCommonService.wildConfig(url).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new MySubscriber<WildConfigResponse>(pMyback));
    }

    public static void reqClassInfoList(final MyHttpback<ClassInfoListResponse> pMyback) {
        WSCommonService wsCommonService = getRetrofitInstance("http://112.74.195.215:100/VHKM/WSCommon.asmx/").create(WSCommonService.class);
        String token = "1";
        wsCommonService.GetClassInfoList(token).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new MySubscriber<ClassInfoListResponse>(pMyback));
    }

    public static void reqKMInfoList(final MyHttpback<KMInfoListResponse> pMyback) {
        WSCommonService wsCommonService = getRetrofitInstance("http://112.74.195.215:100/VHKM/WSCommon.asmx/").create(WSCommonService.class);
        String token = "1";
        wsCommonService.GetKMInfoList(token).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new MySubscriber<KMInfoListResponse>(pMyback));
    }

}


class MySubscriber<T> extends Subscriber {

    private final MyHttpback<T> myback;

    public MySubscriber(MyHttpback<T> pMyback) {
        this.myback = pMyback;
    }

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {
        myback.onErrorBack(e);

    }

    @Override
    public void onNext(Object pO) {
        Log.e("MyHttpback","onSuccessBack--->onSuccessBack！！！");
        myback.onSuccessBack((T) pO);

    }
}
